﻿namespace PlugNT.Common
{
    using ICSharpCode.SharpZipLib.Zip;
    using System;
    using System.IO;

    public class FileHelper
    {

        public static void ZipFile(string topDirName, string[] fileNamesToZip, string ZipedFileName, int CompressionLevel, string password, string comment)
        {
            using (ZipOutputStream s = new ZipOutputStream(System.IO.File.Open(ZipedFileName, FileMode.Create)))
            {
                if (password != null && password.Length > 0)
                    s.Password = password;

                if (comment != null && comment.Length > 0)
                    s.SetComment(comment);

                s.SetLevel(CompressionLevel); // 0 - means store only to 9 - means best compression  
                foreach (string file in fileNamesToZip)
                {
                    using (FileStream fs = File.OpenRead(Path.Combine(topDirName, file)))   //打开待压缩文件  
                    {
                        byte[] buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);      //读取文件流  
                        ZipEntry entry = new ZipEntry(file);    //新建实例  
                        entry.DateTime = DateTime.Now;
                        entry.Size = fs.Length;
                        fs.Close();
                        s.PutNextEntry(entry);
                        s.Write(buffer, 0, buffer.Length);
                    }
                }
                s.Finish();
                s.Close();
            }
        }


        public static void UnZipFile(string zipfileName, string UnZipDir, string password)
        {
     

            ZipInputStream s = new ZipInputStream(File.OpenRead(zipfileName));
            if (password != null && password.Length > 0)
                s.Password = password;
            try
            {
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string directoryName = Path.GetDirectoryName(UnZipDir);
                    string pathname = Path.GetDirectoryName(theEntry.Name);
                    string fileName = Path.GetFileName(theEntry.Name);

                    //生成解压目录   
                    pathname = pathname.Replace(":", "$");//处理压缩时带有盘符的问题  
                    directoryName = directoryName + "\\" + pathname;
                    Directory.CreateDirectory(directoryName);

                    if (fileName != String.Empty)
                    {
                        //解压文件到指定的目录  
                        FileStream streamWriter = File.Create(directoryName + "\\" + fileName);
                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }

                            else
                            {
                                break;
                            }

                        }
                        streamWriter.Close();
                    }
                }
                s.Close();
            }
            catch (Exception eu)
            {
                throw eu;
            }

            finally
            {
                s.Close();
            }
        }
    
    }
}

